package visualcbir.visualstructures.ring;

import visualcbir.*;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.Observable;
import visualcbir.node.ModelNode;
import visualcbir.node.ViewNode;

/**
 *
 * @author sheila
 */
public class VSRings extends ViewComposed{
    public VSRings(ModelComposed oModel, int piNumSubViews){
        super(oModel,piNumSubViews);
    }
    
    @Override
    public  IBackStructureView makeBackStructureView(){
        return new VSRingBackStructureView();
    }

    /* Computes mathematical and trigonometric data (location of the things) needed to draw each object in the view.
     * Sets the points needed to draw images and backstructure. Backstructure is created
     * r = k * Theta ^ a
     * Creates the circunferences needed to put the given images.
     * Each image is located in an angle of the circunference.
     * The angle increment between images is decresed in sucessives circles.
     */
    @Override
    public void update(Observable o, Object arg) {//this.oModel.notifyObservers();
        double x, y, radio=0, k=130, thetaRadianes=0.0;
        int iNodeNumber=0;   
        int iNumNodesByRotation = 3;
        double scaleFactorSubViews = 0.85 * this.getScaleFactor();
        //shi//talvez seja melhor somente escar todas as subvistas num metodo comun independente de ser nodes ou otras vistas

        ArrayList<Polygon> aoPolygons=new ArrayList<Polygon>();
        Polygon oCurrentPolygon;

        // Needed rotations for showing the given images
        for (int icurrentRotation = 0 ; iNodeNumber < ((ModelComposed)oModel).getNumModels() &&  iNodeNumber< this.iNumSubviews; icurrentRotation++) {
            int angleNode = 0;
            int angleIncrement = Math.round( 360/iNumNodesByRotation );

            oCurrentPolygon = new Polygon();
            aoPolygons.add(oCurrentPolygon);
            
            for(double theta=0 ; theta<360 ;theta+=1){
                thetaRadianes = theta * (Math.PI/180);
                x = ( radio * Math.cos(thetaRadianes) + this.getCenterPoint().x );// * getScaleFactor(); FUNCIONOUUUU AQUIIIIIIIIIIIIn
                y = ( radio * Math.sin(thetaRadianes) + this.getCenterPoint().y );// * getScaleFactor();
                oCurrentPolygon.addPoint((int)Math.round(x),(int)Math.round(y));
                
                if( theta == angleNode && iNodeNumber < ((ModelComposed)oModel).getNumModels() && iNodeNumber< this.iNumSubviews){
                    super.getView(iNodeNumber).setCenterPoint((int)Math.round(x),(int)Math.round(y));
                    super.getView(iNodeNumber).setScaleFactor(scaleFactorSubViews-=0.008);
                    iNodeNumber++; angleNode += angleIncrement;
                }
                if(radio==0) break; // first circunference degenerated -> only one image
            }
            
            radio += k * this.getScaleFactor();
            k-= 20;            
            iNumNodesByRotation +=3;
        }
        
        super.oIBackStructureView.setObject(aoPolygons);//3
        super.setViewDimension((int)radio*2+150, (int)radio*2+150);
        
        super.update(o,arg);// updating all subviews.
    }
    
    @Override
    public void addView(Model oModelAdded) {
       if(oModelAdded instanceof ModelNode)
            super.addView(new ViewNode((ModelNode)oModelAdded));
       //else if(oModelAdded instanceof ModelComposed)
       //    super.addView(new VSRings((ModelComposed)oModelAdded,13));
       
    }
}
